package com.semeureka.fault.mina;

import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
import org.apache.mina.core.filterchain.IoFilterChainBuilder;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.demux.DemuxingProtocolDecoder;
import org.apache.mina.filter.codec.demux.DemuxingProtocolEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.semeureka.fault.bean.Rawdata;

@Configuration
public class FilterChainBuilderConfig {
	@Autowired
	private GprsMessageDecoder gprsMessageDecoder;
	@Autowired
	private P101MessageDecoder p101MessageDecoder;
	@Autowired
	private SmsMessageDecoder smsMessageDecoder;
	@Autowired
	private RawdataEncoder messageEncoder;

	@Bean
	public IoFilterChainBuilder gprsFilterChainBuilder() throws Exception {
		DefaultIoFilterChainBuilder filterChainBuilder = new DefaultIoFilterChainBuilder();
		DemuxingProtocolDecoder demuxingProtocolDecoder = new DemuxingProtocolDecoder();
		demuxingProtocolDecoder.addMessageDecoder(gprsMessageDecoder);
		demuxingProtocolDecoder.addMessageDecoder(p101MessageDecoder);
		DemuxingProtocolEncoder demuxingProtocolEncoder = new DemuxingProtocolEncoder();
		demuxingProtocolEncoder.addMessageEncoder(Rawdata.class, messageEncoder);
		filterChainBuilder.addLast("protocolFilter", new ProtocolCodecFilter(demuxingProtocolEncoder,
				demuxingProtocolDecoder));
		return filterChainBuilder;
	}

	@Bean
	public IoFilterChainBuilder smsFilterChainBuilder() throws Exception {
		DefaultIoFilterChainBuilder filterChainBuilder = new DefaultIoFilterChainBuilder();
		DemuxingProtocolDecoder demuxingProtocolDecoder = new DemuxingProtocolDecoder();
		demuxingProtocolDecoder.addMessageDecoder(smsMessageDecoder);
		
		filterChainBuilder.addLast("protocolFilter", new ProtocolCodecFilter(new DemuxingProtocolEncoder(),
				demuxingProtocolDecoder));
		return filterChainBuilder;
	}
}
